package cn.com.mypm.framework.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.apache.log4j.Logger;

import cn.com.mypm.framework.common.config.PropertiesBean;
import cn.com.mypm.framework.common.util.Context;

public class UpgradeTool {

	private PreparedStatement ps = null;
	public Connection conn = null;
	public String compId = "";
	Statement st = null;
	private static Logger loger = Logger.getLogger(UpgradeTool.class);

	public UpgradeTool() {
		PropertiesBean conf = (PropertiesBean) Context.getInstance().getBean("ContextProperties");
		String driver = conf.getProperty("config.db.driver");
		String url = conf.getProperty("config.db.url");
		String userName = conf.getProperty("config.db.user");
		String password = conf.getProperty("config.db.password");
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName, password);
			conn.setAutoCommit(false);
			st = conn.createStatement();
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			loger.error(e);
			e.printStackTrace();
		}
	}


	public void upgradeDb(String pathAndFile) {
		OutputStreamWriter sqlFileWrite = null;
		InputStreamReader fr = null;
		try {
			 fr = new InputStreamReader(new FileInputStream(
					pathAndFile), "utf-8");
			BufferedReader br = new BufferedReader(fr);
			String line = br.readLine();
			StringBuffer sql = new StringBuffer();
			while (line != null) {
				if (!line.equals("") && !line.startsWith("#")) {
					if(line.endsWith(";")){
						sql.append(line);
						st.execute(sql.toString().substring(0, sql.length()-1));
						sql.delete(0, sql.length()-1);
					}else{
						sql.append(line);
					}
				}
				line = br.readLine();
			}
			fr.close();
			File sqlFile = new File(pathAndFile);
			sqlFileWrite = new OutputStreamWriter(
					new FileOutputStream(sqlFile), "utf-8");
			sqlFileWrite.write("");
			conn.commit();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(st!=null){
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}	
			if(fr!=null){
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(sqlFileWrite!=null){
				try {
					sqlFileWrite.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}








}
